//
//
//
//
//

#include "Socks4Util.h"
#include "NetCDR.h"

namespace Socks4Util
{
	//
	//
	//
	bool ParseSocks4Cmd(const char* buf,
			U32 length,
			U8& version,
			U8& cmd,
			U16& port,
			U32& ip)
	{
		if(NULL == buf)
			return false;

		NetCDR outCDR(buf, length);
		outCDR >> version;
		outCDR >> cmd;
		outCDR >> port;
		outCDR >> ip;
		return true;
	}

	//
	//
	// parse with uid
	bool ParseSocks4Cmd(const char* buf,
			U32 length,
			U8& version,
			U8& cmd,
			U16& port,
			U32& ip,
			char* uid)
	{
		if(NULL == buf)
			return false;
		NetCDR outCDR(buf, length);
		outCDR >> version;
		outCDR >> cmd;
		outCDR >> port;
		outCDR >> ip;
		if(NULL != uid)
			outCDR >> uid;
		return true;
	}

	bool ConSocks4Cmd(char* buf,
			U8 version,
			U8 cmd,
			U16 port,
			U32 ip)
	{
		if(NULL == buf)
			return false;

		NetCDR outCDR(128);
		outCDR << version << cmd << port << ip;
		memcpy(buf, outCDR.Data(), outCDR.Size());
		return true;
	}

	bool ConSocks4Cmd(char* buf,
			U8 version,
			U8 cmd,  
			U16 port, 
			U32 ip,
			const char* uid)
	{
		if(NULL == buf)
			return true;

		NetCDR outCDR(128);
		outCDR << version << cmd << port << ip << uid;
		memcpy(buf, outCDR.Data(), outCDR.Size());
		return true;
	}
};
